<?php
	/*
 	* @version $Id: agentup.php,v 1.2.2 2010/10/02 09:37:56 marco_manti
 	* @package http://www.gisa.by
 	* @copyright (C) 2006 - 2010 marco_manti(R)
 	* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
 	* @description Script replace date fields for some database objects
 	*
 	*/

 	// get library
	require_once(dirname(dirname(__FILE__)).'/includes/library.php');

        // init global classes
        $config = new CONFIG();
        $dbo = new DB();
        $helper = new HELPER();
        
        // globals
        $endline = chr(13).chr(10);
        $counter = 0;
        $today = date('Y-m-d H:i');
        $title = ' UPDATE AGENT: ';
        $email_title = 'GISA.by ::' . $title;
        $email = $config->server_root;
        $day  = strtotime($today);
        $day += 60 * 60 * 24 + 1;
        $tomorow = date('Y-m-d', $day);
        $logmsg = $mysqlerrlog = '';

        // mail headers
        $headers  = "MIME-Version: 1.0\r\n";
        $headers .= "Content-type: text/html; charset=windows-1251\r\n";
        $headers .= "From: GISA.by <gisaby@gmail.com>\r\n";
        $headers .= "X-Mailer: PHP/" . phpversion();

        // connect to db & get data
	if (!$dbo->connect()) {
            $logmsg .= $today.$title.'ERROR - connect to database ['.$dbo->error().'];'.$endline;
            $mysqlerrlog .= $today.$title.' '.$dbo->error().$endline;
            mail($email, $email_title, 'ERROR - connect to database ['.$dbo->error().'];', $headers);
        }

        $query = " UPDATE rlt_estateagent AS e "
               . " JOIN rlt_comprofiler AS c "
               . " ON c.user_id = e.created_by "
               . " SET e.publish_up = '".date('Y-m-d')."', e.publish_down = '".date('Y-m-d', time() + (60 * 60 * 24 * 7))."' "
               . " WHERE c.cb_agent > 0";
        
        $rows = $dbo->query($query);
        if ($dbo->_lastResult < 0) {
            $logmsg .= $today.$title.'ERROR - update fields ['.$dbo->error().'];'.$endline;
            $mysqlerrlog .= $today.$title.' '.$dbo->error().$endline;
            mail($email, $email_title, 'ERROR - update fields ['.$dbo->error().'];', $headers);
        } else {
            $logmsg .= $today.$title.'SUCCESS - '.$rows.' fields updated;'.$endline;
        }

        // write state to log
        $helper->writeToLog($logmsg);

        // write SQL error if present
        if ($mysqlerrlog != '') {
            $helper->writeToFile($mysqlerrlog, 'a+', 'mysql_errors.log');
        }
?>